package lib.XML;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

import java.util.ArrayList;

import lib.Base.Friend;


public class FriendlistContentHandler implements ContentHandler {
	/**
	 * Class to handle XML content for Friendlist entrys
	 */
	private String currentValue;
	private ArrayList<Friend> Friendlist;
	
	private Friend friend;

	public void characters(char[] ch, int start, int length)
			throws SAXException {
		currentValue = new String(ch, start, length);
	}

	public void startElement(String uri, String localName, String qName, Attributes atts) 
			throws SAXException {
		if (localName.equals("friendlist")) {
			Friendlist = new ArrayList<Friend>();
		}else if(localName.equals("friend")){
			friend = new Friend(atts.getValue("id"));
		}
	}
	
	public void endElement(String uri, String localName, String qName)
		      throws SAXException {
		if (localName.equals("name")) {
			friend.setName(currentValue);
		}else if (localName.equals("friend")) {
		      Friendlist.add(friend);
		}
	}
	
	public ArrayList<Friend> getFriendlist(){
		return Friendlist;
	}
	
	
	public void endDocument() throws SAXException {}
	public void endPrefixMapping(String prefix) throws SAXException {}
	public void ignorableWhitespace(char[] ch, int start, int length)
			throws SAXException {}
	public void processingInstruction(String target, String data)
			throws SAXException {}
	public void setDocumentLocator(Locator locator) {  }
	public void skippedEntity(String name) throws SAXException {}
	public void startDocument() throws SAXException {}
	public void startPrefixMapping(String prefix, String uri)
			throws SAXException {}
}